<?php
class retrieve_group extends retrieve_base{

	private $module_display_options 	= array(
	);
	
/**
	* function produces abstract structure of modules db structure
	*
    * used to generate the table structure required abstract function that passes an abstract
	* representation of the desired table structure to the proper database module which will
	* interpet the abstract and convert it into a valid SQL Create table structure.
	*
	* @return Array list of abstract table definitions for this module
	*/
	function create_table(){
		$tables = array();
		/**
		* Table structure for table 'group_data'
		*/
		$fields = array(
			array("group_identifier"	,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("group_client"		,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("group_label"			,"varchar(255)"				,"NOT NULL"	,"default ''"),
			array("group_type"			,"unsigned small integer"	,"NOT NULL"	,"default '1'"),
			array("group_default"		,"unsigned small integer"	,"NOT NULL"	,"default '0'")
		);
		$primary ="group_identifier";
		$tables[count($tables)] = array("group_data", $fields, $primary);
		/**
		* Table structure for table 'group_access'
		*/
		$fields = array(
			array("access_group"		,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("access_code"			,"varchar(255)"				,"NOT NULL"	,"default ''")
		);
		$primary ="";
		$tables[count($tables)] = array("group_access", $fields, $primary);
		
		/**
		* Table structure for table 'group_types'
		*/
		$fields = array(
			array("group_type_identifier"	,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("group_type_label"		,"varchar(255)"				,"NOT NULL"	,"default ''")
		);
		$primary ="group_type_identifier";
		$tables[count($tables)] = array("group_type", $fields, $primary, $this->system_default_data("TYPE"));
		/**
		* Table data for table 'group'
		*/
		
		/**
		* Table structure for table 'menu_groups'
		*/
		$fields = array(
			array("group_identifier"	,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("menu_identifier"		,"unsigned integer"	,"NOT NULL"	,"default '0'")
		);
		$primary ="";
		$tables[count($tables)] = array("relate_menu_groups", $fields, $primary);
		/**
		* Table structure for table 'document_groups'
		*/
		$fields = array(
			array("group_identifier"	,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("document_identifier"	,"unsigned integer"	,"NOT NULL"	,"default '0'")
		);
		$primary ="";
		$tables[count($tables)] = array("relate_document_groups", $fields, $primary);
		/**
		* Table structure for table 'group_data'
		*/
		$fields = array(
			array("group_identifier"	,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("menu_identifer"		,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("user_identifier"		,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("client_identifier"	,"unsigned integer"	,"NOT NULL"	,"default '0'")
		);
		$primary ="";
		$tables[count($tables)] = array("group_admin_menu_access", $fields, $primary);
		/**
		* Table structure for table 'group_access_to_page'
		-
		* This is a relationship table. 
		-
		* This table is used to specify the groups that can see a particular version of a document
		* this table works with the version control software to mark the groups that can see each 
		* version, there fore if you roll back a document you will still have the groups that were
		* assigned to that version of the document in that lanugage
		*/
		$fields = array(
			array("group_identifier"	,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("trans_identifier"	,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("client_identifier"	,"unsigned integer"	,"NOT NULL"	,"default '0'")
		);
		$primary ="";
		$tables[count($tables)] = array("group_access_to_page", $fields, $primary);

		/**
		* Table structure for table 'groups_belonging_to_user'
		-
		* This is a relationship table. 
		-
		* This table is used to specify the groups that a user belongs to
		*/
		$fields = array(
			array("uid"					,"unsigned integer"	,"NOT NULL"	,"auto_increment"),
			array("group_identifier"	,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("user_identifier"		,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("client_identifier"	,"unsigned integer"	,"NOT NULL"	,"default '0'")
		);
		$primary ="uid";
		$tables[count($tables)] = array("groups_belonging_to_user", $fields, $primary);
		/**
		* Table structure for table 'groups_containing_group'
		*
		* This is a relationship table. 
		*
		* This table is used to specify the groups that contain other groups
		*/
		$fields = array(
				array("uid"					,"unsigned integer"	,"NOT NULL"	,"auto_increment"),
				array("group_identifier"	,"unsigned integer"	,"NOT NULL"	,"default '0'"),
				array("contains_group"		,"unsigned integer"	,"NOT NULL"	,"default '0'"),
				array("client_identifier"	,"unsigned integer"	,"NOT NULL"	,"default '0'")
		);
		$primary ="uid";
		$tables[count($tables)] = array("groups_containing_group", $fields, $primary);
		/**
		* Table structure for table 'groups_to_object'
		*
		* This table is used to specify the objects that are contained in a group
		*/
		$fields = array(
				array("gto_identifier"	,"unsigned integer"	,"NOT NULL"	,"default '0'"),
				array("gto_client"		,"unsigned integer"	,"NOT NULL"	,"default '0'"),
				array("gto_object"		,"unsigned integer"	,"NOT NULL"	,"default '0'"),
				array("gto_module"		,"varchar(255)"		,"NOT NULL"	,"default ''"),
				array("gto_rank"		,"unsigned integer"	,"NOT NULL"	,"default '0'")
		);
		$primary ="gto_identifier";
		$tables[count($tables)] = array("group_to_object", $fields, $primary);
		
		return $tables;
	}	

	function system_default_data($type=""){
		$sql = array();
		$i=0;
		if ($type=="TYPE"){
			$sql[$i++] = "INSERT INTO group_type (group_type_label) VALUES('LOCAL_GROUP_BASIC');";
			$sql[$i++] = "INSERT INTO group_type (group_type_label) VALUES('LOCAL_GROUP_ADMIN');";
		}
		return $sql;
	}

	function create_client_details($parameters){

		$client_identifier = $this->check_parameters($parameters,"client_identifier",-1);
		/**
		* Table data for table 'group_data'
		*/
		$this->db->query("INSERT INTO group_data (group_client, group_label, group_type, group_default) VALUES('$client_identifier', 'Registered',1,1);");
		$this->db->query("INSERT INTO group_data (group_client, group_label, group_type, group_default) VALUES('$client_identifier', 'Administrator',2,0);");
		if (($this->parent->server[LICENCE_TYPE]==ECMS)||($this->parent->server[LICENCE_TYPE]==MECM)){
			$this->db->query("INSERT INTO group_data (group_client, group_label, group_type, group_default) VALUES('$client_identifier', 'Author',2,0);");
			$this->db->query("INSERT INTO group_data (group_client, group_label, group_type, group_default) VALUES('$client_identifier', 'Approver',2,0);");
			$this->db->query("INSERT INTO group_data (group_client, group_label, group_type, group_default) VALUES('$client_identifier', 'Level 1',1,0);");
			$this->db->query("INSERT INTO group_data (group_client, group_label, group_type, group_default) VALUES('$client_identifier', 'Level 2',1,0);");
		}
		$sql = "select * from group_data where group_client = $client_identifier and group_type=2;";
		$result = $this->db->query($sql);
		$list = Array();
		while ($r = $this->db->fetch_array($result)){
			$list[$r["group_label"]] = $r["group_identifier"];
		}
		/**
		* Table data for table 'group_access'
		*/
		if ($this->check_parameters($list,"Administrator","__NOT_FOUND__")!="__NOT_FOUND__"){
			$this->db->query("INSERT INTO group_access (access_group,access_code) VALUES('".$list["Administrator"]."', 'ALL');");
		}
		if ($this->check_parameters($list,"Author","__NOT_FOUND__")!="__NOT_FOUND__"){
			$this->db->query("INSERT INTO group_access (access_group,access_code) VALUES('".$list["Author"]."', 'PAGE_AUTHOR');");
			$this->db->query("INSERT INTO group_access (access_group,access_code) VALUES('".$list["Author"]."', 'FILES_ALL');");
		}
		if ($this->check_parameters($list,"Approver","__NOT_FOUND__")!="__NOT_FOUND__"){
			$this->db->query("INSERT INTO group_access (access_group,access_code) VALUES('".$list["Approver"]."', 'PAGE_PUBLISHER');");
		}
	}

}
?>